package com.ml.mall.conversion;

import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.res.admin.AdminPurchaseRes;
import com.ml.mall.bean.res.admin.vo.PurchaseOrderArrVO;
import com.ml.mall.bean.res.admin.vo.PurchaseSpentReportVO;
import com.ml.mall.bean.res.supplier.SupplierPurchaseQueryPageRes;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.utils.DateTransformUtils;
import org.apache.tomcat.jni.BIOCallback;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/9 23:54
 */

public class AnalyticsSupplierServiceHelp {


    public static List<SupplierPurchaseQueryPageRes> supplierPurchaseQueryTopConversion(BaseDateRangeReq req, Future<List<DateValueVO>> purchaseSpentArrFuture) throws ExecutionException, InterruptedException {
        List<SupplierPurchaseQueryPageRes> resList = new ArrayList<>();
        if(purchaseSpentArrFuture!=null){
            List<DateValueVO> dateValueVOS = purchaseSpentArrFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    SupplierPurchaseQueryPageRes purchaseOrderArrVO = new SupplierPurchaseQueryPageRes();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseOrderArrVO.setCompanyName(key);
                    Double v = value.stream().mapToDouble(e->e.getValue().doubleValue()).sum();
                    purchaseOrderArrVO.setTotalSpent(BigDecimal.valueOf(v));
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseOrderArrVO.setDayDetail(curDateMonthDataInit);
                    resList.add(purchaseOrderArrVO);
                }
            }
            resList = resList.stream().sorted(Comparator.comparing(SupplierPurchaseQueryPageRes::getTotalSpent).reversed()).collect(Collectors.toList());
            if(resList.size()>10){
                resList = resList.subList(0,10);
            }

        }
        return resList;

    }
}
